import allure
import pytest

from config import BaseDir
from page.MediaPage.PageHome import HomeProxy
from page.MediaPage.PageLogin import LoginProxy
from page.MediaPage.PagePublish import PublishProxy
from utils import UtilsDriver, is_exist, get_case_data

login_data = get_case_data(BaseDir + "/data/MediaJson/login.json")
publish_data = get_case_data(BaseDir + "/data/MediaJson/publish.json")


@pytest.mark.run(order=1)
class TestMedis:
    # 定义类级别的fixture初始化方法
    def setup_class(self):
        self.login_proxy = LoginProxy()
        self.home_proxy = HomeProxy()
        self.publish_proxy = PublishProxy()

    # 定义类级别的fixture的销毁方法
    def teardown_class(self):
        UtilsDriver.quit_media_driver()

    # 测试登录
    @allure.severity(allure.severity_level.BLOCKER)    #严重级别
    @pytest.mark.parametrize("username,code,expect", login_data)
    def test_login(self, username, code, expect):
        self.login_proxy.login(username, code)
        # 使用allure保存截图
        allure.attach(UtilsDriver.get_media_driver().get_screenshot_as_png(), "截图", allure.attachment_type.PNG)
        msg = self.home_proxy.get_username_msg()
        assert expect == msg

    # 测试发表文章
    @pytest.mark.parametrize("title,content,channel,expect", publish_data)
    def test_publish(self, title, content, channel, expect):
        self.home_proxy.go_publish_article()
        self.publish_proxy.publish_article(title, content, channel)
        assert is_exist(UtilsDriver.get_media_driver(), expect)
